new;

library pgraph;

startdate_prd=1979; /*** Year of first intepolated value ***/

frq=4; /** Frequency of observations, here quarterly **/


fname = "c:\\gauss6.0\\Interpolation\\ExampleSheet.xls";


/****** Name of Variables and Dates for Output *****/ 
range1 = "a3:a114"; 
range2 = "b1:l1";

dates = xlsreadsa(fname, range1, 1, "");
namVariables = xlsreadsa(fname, range2, 2, "");

/************************************************/

range3 = "b2:l114"; 
range4 = "b2:l34";

y = xlsreadm(fname, range3, 1, "");
intp = xlsreadm(fname, range4, 2, "");

      
{y_new}=interpol(y,intp,startdate_prd,frq);


/**ygdp=ln(ygdp);**/


/** Exporting the output to an excel sheet **/

fname ="c:\\gauss6.0\\Interpolation\\InterpolatedSeries.xls";
ret = xlswrite(y_new, fname,"b2",1,"");
fname ="c:\\gauss6.0\\Interpolation\\InterpolatedSeries.xls";
ret = xlswrite(dates, fname,"a2",1,"");
fname ="c:\\gauss6.0\\Interpolation\\InterpolatedSeries.xls";
ret = xlswrite(namVariables, fname,"b1",1,"");







/*** Procedures ***/


/*
**> interpol
**
**  Purpose: Performs Annual to Quarterly interpolation for GDP variables or   
**           other variables of similar time series characteristics
**
**  Format:     {y}=interpol(x,intp,samplestartdate,frq)
**
**  Inputs:     x             :  yearly series that will be used for interpolation
**
**              intp          :  Specifies forward or backward interpolation.
**                               
**                               1 corresponds to forward interpolation 
**
**                               Note: Relevant when no quarterly data available over 
**                               the sample period of interest, in which case an extra
**                               annual observation is required immediately preceding
**                               the period of interest to set off the recursion 
**
** 
**                               0 corresponds to backward interpolation    
**
**              samplestartdate: Starting year of the sample period of interest 
**
**              frq            : Frequency of observations to be interpolated. 
**                               For quarterly frequency frq=4
**
**
**
**  Output:     y : quarterly interpolated series
**
**
**
*/


proc(1)=interpol(ysa1,intp,samplestartdate,frq);

local kint,indint,typeint,quartBint,startint,ym,numyears,j,y,my,
yms,yint,yms1,zintp,zp,p,iniyearBint,yaux,datevar,ydt,e1,vind,ysa,
ydt1,y1,y2,z,zq,j1,yn;


ysa=ysa1[2:rows(ysa1),.];

/*** interpolation file ****/

kint=4; /*** the number of rows in excel files relating to information excluding the
          names of the countries and including the initial value for forward interpolation***/
typeint=2; /**	0 for backward interpolation / 1 for forward **/
quartBint=3;  /** quarter to start backward interpolation **/
startint=4;	/** year to start backward interpolation **/


yint=-99999*ones(rows(ysa),cols(ysa)); 

numyears=rows(yint)/4;  /* This needs to be modified if the date starts and finishes at different 
                           quarters than the first and fourth. This will affect the datevar ***/

j=1;
do while j<=cols(ysa);

y=ysa[.,j];
my= ismiss(y);
yint[.,j]=y;

if my/=0;

ym=missrv(y,-99999);

vind = indexcat(ym,-99999);
zintp=intp[kint+1:rows(intp),j];
zp=packr(zintp);
z=zp;


p=intp[quartBint,j];

iniyearBint=intp[startint,j];
datevar=seqa(samplestartdate,1,numyears) .*. ones(frq,1);

if intp[typeint,j]==0;

ydt=datevar~y;

e1=(ydt[.,1] .eq iniyearBint);
ydt1=selif(ydt,e1);
y1=ydt1[.,2];

if p==1;
y2=y1[1];
elseif p/=1;
y2=y1[p:rows(y1)];
endif;

z=zp|y2;

endif;

if intp[typeint,j]==1;

{zq}=Atoq_interpolationF(z);

elseif intp[typeint,j]==0;

{zq}=Atoq_interpolationB(z,p);

endif;

yint[vind,j]=zq; 

endif;

j=j+1;
endo;

retp(yint);
endp;



proc(1)= AToqf(X,root);

local Xn,m,ym,xtm,s,lambda,rho,i,r,ym_1,yms,z,a3,a2,a1,a0,j,epsilon,df,rho4;

Xn=X[2:rows(X)];

m=4;  

ym=-99999*ones(m*(rows(X)-1),1); /** first quarterly observation kept for initial value **/


xtm=X[1]/m;


s=1;
do while s<=rows(X)-1;

lambda=X[1+s]/xtm;

a3=1;
a2=1;
a1=1;
a0=-lambda;



{rho4}=quartic_eq(a0,a1,a2,a3,root);

i=1;
do while i<=m;

ym[(s-1)*m+i]=xtm*(rho4^i);


i=i+1;
endo;


xtm=ym[s*m];


s=s+1;
endo;


r=rows(ym)/m;
ym_1 = reshape(ym,r,m);
ym_1=ym_1';


yms=sumc(ym_1);

epsilon=0.0000001;
j=1;
do while j<=rows(Xn);

df=Xn[j]-yms[j];

if (abs(df)>epsilon);
print "errorlog: Sum of quarterly observations not equal to annual";
stop;
endif;

j=j+1;
endo;

ym=m*ym;

retp(ym);
endp;




proc(1)= AToqb(X,p,root);

local m, ym, xt1, s, lambda, rho, i, r, ym_1, yms, z,a3,a2,a1,a0,b2,b1,b0,rho3,
j, epsilon, df, rho4, x0,c,ck,ck1,ymsini,sm,rho2;


m=4; 

if p==1;
x0=X[rows(X)]/4;

X=X[1:rows(X)-1];

elseif p/=1;
x0=X[rows(X)-(m-p)]/4;

if (p==2 or p==3);
sm=sumc(X[rows(X)-(m-p)+1:rows(X)])/4;
endif;
X=X[1:rows(X)-(m-p+1)];
endif;



if p==1;
ym=-99999*ones(m*rows(X),1); 
elseif p/=1;
ym=-99999*ones(m*(rows(X)-1)+(p-1),1); 
if p==4;
X[rows(X)]=X[rows(X)]-x0;
elseif (p==2 or p==3);
X[rows(X)]=X[rows(X)]-x0-sm;
endif;
endif;

xt1=x0;

s=rows(X)+1;
do while s>=2;

lambda=X[s-1]/xt1;

a3=1;
a2=1;
a1=1;
a0=-lambda;


b2=a2;
b1=a1;
b0=a0;

if (s==(rows(X)+1) and p==4);

{rho3}=cubic_eq(b0,b1,b2);

rho4=rho3;

elseif (s==(rows(X)+1) and p==3);

{rho2}=quadratic_eq(b0,b1);

rho4=rho2;

elseif (s==(rows(X)+1) and p==2);

rho4=lambda;

elseif ((s==(rows(X)+1) and p==1) or s/=(rows(X)+1));

{rho4}=quartic_eq(a0,a1,a2,a3,root);

endif;


c=m-1;
ck=0;
if (s==(rows(X)+1) and p/=1);
c=p-2;
ck=c+1;
ck1=c+(m+2-p);
elseif (s/=(rows(X)+1) and p/=1);
c=m-1;
ck=m;
ck1=ck;
endif;


i=0;
do while i<=c;

if p==1;
ym[(s-1)*m-i]=xt1*(rho4^(i+1));
elseif p/=1;
ym[ck+(s-2)*m-i]=xt1*(rho4^(i+1));
endif;


i=i+1;
endo;

if p==1;
xt1=ym[(s-1)*m-(m-1)];
elseif p/=1;
xt1=ym[ck1+(s-2)*m-(m-1)];
endif;
s=s-1;
endo;




if p==1;
r=rows(ym)/m;
ym_1 = reshape(ym,r,m);
ym_1=ym_1';
yms=sumc(ym_1);
elseif p/=1;
r=rows(ym[1:(rows(ym)-(p-1))])/m;
ym_1 = reshape(ym[1:(rows(ym)-(p-1))],r,m);
ym_1=ym_1';
ymsini=sumc(ym[rows(ym)-(p-1)+1:rows(ym)]);
yms=sumc(ym_1);
yms=yms|ymsini;
endif;


epsilon=0.0000001;

j=1;
do while j<=rows(X);

df=X[j]-yms[j];


if (abs(df)>epsilon);
print "errorlog: Sum of quarterly observations not equal to annual";
stop;
endif;


j=j+1;
endo;


ym=m*ym;

retp(ym);
endp;




proc(1)= root1_quartic(D1,D2,k);

local rho4d1, rho4_1d1, rho4_2d1, rho4d2, rho4_1d2, rho4_2d2, r_1, r_2,  rho4;

if (D1==0 and D2<0);
rho4d1=(-k)/2;
rho4=rho4d1;
elseif (D1>0 and D2<0);
rho4_1d1=(-k+sqrt(D1))/2; 
rho4_2d1=(-k-sqrt(D1))/2;
if (rho4_1d1>0 and rho4_2d1<0);
rho4=rho4_1d1;
elseif (rho4_1d1<0 and rho4_2d1>0);
rho4=rho4_2d1;
elseif (rho4_1d1>0 and rho4_2d1>0);
print " There are two positive roots";
if (rho4_1d1>rho4_2d1);
rho4=rho4_1d1;
elseif (rho4_2d1>rho4_1d1);
rho4=rho4_2d1;
endif;
endif;
elseif (D2==0 and D1<0);
rho4d2=-((-k)/2);
rho4=rho4d2;
elseif (D2>0 and D1<0);
rho4_1d2=(-(-k)+sqrt(D2))/2; 
rho4_2d2=(-(-k)-sqrt(D2))/2; 
if (rho4_1d2>0 and rho4_2d2<0);
rho4=rho4_1d2;
elseif (rho4_1d2<0 and rho4_2d2>0);
rho4=rho4_2d2;
elseif (rho4_1d2>0 and rho4_2d2>0);
print " There are two positive roots";
if (rho4_1d2>rho4_2d2);
rho4=rho4_1d2;
elseif (rho4_2d2>rho4_1d2);
rho4=rho4_2d2;
endif;
endif;
elseif (D1==0 and D2==0);
print "There are two real double roots";
r_1=(-k)/2;
r_2=-((-k)/2);
if (r_1>0 and r_2<0);
rho4=r_1;
elseif (r_1<0 and r_2>0);
rho4=r_2;
elseif (r_1>0 and r_2>0);
print " There are two positive roots";
if (r_1>r_2);
rho4=r_1;
elseif (r_1<r_2);
rho4=r_2;
endif;
endif;
elseif (D1>0 and D2>0);
print "There are four real roots";
stop;
elseif (D1<0 and D2<0);
print "There are no real roots";
stop;
endif;

retp(rho4);
endp;




proc(1)= root2_quartic(D1,D2,k);

local rho4d1, rho4_1d1, rho4_2d1, rho4d2, rho4_1d2, rho4_2d2, r_1, r_2, rho4;

if (D1==0 and D2<0);
rho4d1=(-k)/2;
rho4=rho4d1;
elseif (D1>0 and D2<0);
rho4_1d1=(-k+sqrt(D1))/2; 
rho4_2d1=(-k-sqrt(D1))/2;
if (rho4_1d1<0 and rho4_2d1>0);
rho4=rho4_1d1;
elseif (rho4_1d1>0 and rho4_2d1<0);
rho4=rho4_2d1;
elseif (rho4_1d1<0 and rho4_2d1<0);
print " There are two negative roots";
if (rho4_1d1>rho4_2d1);
rho4=rho4_1d1;
elseif (rho4_2d1>rho4_1d1);
rho4=rho4_2d1;
endif;
endif;
elseif (D2==0 and D1<0);
rho4d2=-((-k)/2);
rho4=rho4d2;
elseif (D2>0 and D1<0);
rho4_1d2=(-(-k)+sqrt(D2))/2; 
rho4_2d2=(-(-k)-sqrt(D2))/2; 
if (rho4_1d2<0 and rho4_2d2>0);
rho4=rho4_1d2;
elseif (rho4_1d2>0 and rho4_2d2<0);
rho4=rho4_2d2;
elseif (rho4_1d2<0 and rho4_2d2<0);
print " There are two negative roots";
if (rho4_1d2>rho4_2d2);
rho4=rho4_1d2;
elseif (rho4_2d2>rho4_1d2);
rho4=rho4_2d2;
endif;
endif;
elseif (D1==0 and D2==0);
print "There are two real double roots";
r_1=(-k)/2;
r_2=-((-k)/2);
if (r_1>0 and r_2<0);
rho4=r_2;
elseif (r_1<0 and r_2>0);
rho4=r_1;
elseif (r_1<0 and r_2<0);
print " There are two negative roots";
if (r_1>r_2);
rho4=r_1;
elseif (r_1<r_2);
rho4=r_2;
endif;
endif;
elseif (D1>0 and D2>0);
print "There are four real roots";
stop;
elseif (D1<0 and D2<0);
print "There are no real roots";
stop;
endif;
retp(rho4);
endp;


proc(1)=cubic_eq(b0,b1,b2);

local r3,q3,d,rho3;

r3=(9*b2*b1-27*b0-2*(b2^3))/54;
q3=(3*b1-(b2^2))/9;


d=q3^3+r3^2;

if d<=0;
errorlog "ERROR: The cubic equation does not have a unique real root";
stop;
endif;

rho3=((r3+sqrt(d))^(1/3))-(q3/((r3+sqrt(d))^(1/3)))-(b2/3);

retp(rho3);
endp;


proc(1)=quartic_eq(a0,a1,a2,a3,root);

local p4,q4,r4,b2,b1,b0,rho3,k,D1,D2,rho4D,rho4;

/**
print "a0=" a0;
print "a1=" a1;
print "a2=" a2;
print "a3=" a3;
print "root=" root;
**/

p4=a2-(3/8)*(a3^2);

q4=a1-(1/2)*a2*a3+(1/8)*(a3^3);

r4=a0-(1/4)*a1*a3+(1/16)*a2*(a3^2)-(3/256)*(a3^4);


b2=-p4;
b1=-4*r4;
b0=4*p4*r4-(q4^2);


{rho3}=cubic_eq(b0,b1,b2);

k=sqrt(rho3-p4);

/***
x^2+k*x+(1/2)*u1-(q/2*k);
x^2-k*x+(1/2)*u1+(q/2*k);
***/

/**
print "k=" k;
print "q4=" q4;
print "rho3=" rho3;
**/

D1=k^2-4*((1/2)*rho3-(q4/(2*k)));
D2=(-k)^2-4*((1/2)*rho3+(q4/(2*k)));

/**
print "D1=" D1;
print "D2=" D2;
**/

if root==1;
{rho4D}=root1_quartic(D1,D2,k);
elseif root==2;
{rho4D}=root2_quartic(D1,D2,k);
endif;


rho4=(rho4D-(a3/4));

retp(rho4);
endp;




proc(1)=Atoq_interpolationF(y);

local yq1,yq2,yq;

{yq1}=AToqf(y,1);
{yq2}=AToqf(y,2);

{yq}=root_comparison(yq1,yq2);

retp(yq);
endp;

proc(1)=Atoq_interpolationB(y,p);

local yq1,yq2,yq;

{yq1}=AToqb(y,p,1);
{yq2}=AToqb(y,p,2);

{yq}=root_comparison(yq1,yq2);

retp(yq);
endp;

proc(1)=root_comparison(yq1,yq2);

local sum1,i1,sum2,i2,yq;


sum1=abs(ln(yq1[2]/yq1[1]));

i1=3;
do while i1<=rows(yq1);
sum1=sum1+abs(ln(yq1[i1]/yq1[i1-1]));
i1=i1+1;
endo;

sum1=sum1/4;

sum2=abs(ln(yq2[2]/yq2[1]));

i2=3;
do while i2<=rows(yq2);
sum2=sum2+abs(ln(yq2[i2]/yq2[i2-1]));
i2=i2+1;
endo;

sum2=sum2/4;

if sum1< sum2;
yq=yq1;
else;
yq=yq2;
endif;

retp(yq);

endp;


proc(1)=quadratic_eq(b0,b1);

local D,rho1,rho2,rho;


D=b1^2-4*b0;


if D==0;
rho=-b1/2;
elseif D>0;
rho1=(-b1+sqrt(D))/2;
rho2=(-b1-sqrt(D))/2;
if (rho1>=0 and rho2<0);
rho=rho1;
elseif(rho1<0 and rho2>=0);
rho=rho2;
elseif(rho1>0 and rho2>0 and rho1>rho2);
rho=rho1;
endif;
elseif D<0;
print "The quadratic equation has no real roots";
stop;
endif;

retp(rho);
endp;



end;